//
//  DbHandel.m
//  GOGOTravel
//
//  Created by dllo on 16/2/26.
//  Copyright © 2016年 果果. All rights reserved.
//

#import "DbHandel.h"
#import "FMDB.h"
#import "DBsearchModel.h"

@interface DbHandel ()

@property (nonatomic, retain) FMDatabase *dbPoint;

@end

@implementation DbHandel

/** 单例方法 */

+ (DbHandel *)shareDbHandel{
    //1.创建静态指针
    static DbHandel *handel = nil;
    //2.创建对象
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        handel = [[DbHandel alloc] init];
    });
    
    return handel;
}
/** 打开数据库 */
- (void)openDB{
    //获取document路径
    NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    //拼接数据库路径
    NSString *dbPath = [document stringByAppendingPathComponent:@"searchHistory.sqlite"];
    NSLog(@"%@", dbPath);

    //创建数据库
    self.dbPoint = [FMDatabase databaseWithPath:dbPath];
    BOOL result = [_dbPoint open];
    if (result) {
        NSLog(@"打开成功");
    }else{
        NSLog(@"打开失败");
    }
}
/** 建表 */
- (void)createTable{
    //spl语句
    NSString *splStr = [NSString stringWithFormat:@"create table if not exists DBsearchModel(number integer primary key autoincrement, searchText text)"];
        BOOL result = [_dbPoint executeUpdate:splStr];
    
    if (result) {
        NSLog(@"建表成功");
    }else{
        NSLog(@"建表失败");
    }
}
/** 创建登陆表单 */



- (void)createDengLuTable{
    NSString *splStr = [NSString stringWithFormat:@"create table if not exists DengLuTabelModel(number integer primary key autoincrement, userName text, iconURL text)"];
    BOOL result = [_dbPoint executeUpdate:splStr];
    
    if (result) {
        NSLog(@"登陆建表成功");
    }else{
        NSLog(@"登陆建表失败");
    }
}
/** 创建Dailys收藏表单数据库 */

- (void)createDailySCTable{
    
    NSString *splStr = [NSString stringWithFormat:@"create table if not exists ShouChangModel(number integer primary key autoincrement, backImageStr text, hearderImageStr text, titleLabelStr text, nameLabelStr text, spot_id text)"];
    BOOL result = [_dbPoint executeUpdate:splStr];
    
    if (result) {
        NSLog(@"建表成功");
    }else{
        NSLog(@"建表失败");
    }
}


- (void)insertModel:(DBsearchModel *)model{
    //spl语句
    NSString *splStr = [NSString stringWithFormat:@"insert into DBsearchModel(searchText) values('%@')", model.searchText];
    //执行
    BOOL result = [_dbPoint executeUpdate:splStr];
    if (result) {
        NSLog(@"添加成功");
        
    }else{
        NSLog(@"添加失败");
    }

}

- (void)insertDengLuModel:(DengLuTabelModel *)model{
    //spl语句
    NSString *splStr = [NSString stringWithFormat:@"insert into DengLuTabelModel(userName, iconURL) values('%@', '%@')", model.userName, model.iconURL];
    //执行
    BOOL result = [_dbPoint executeUpdate:splStr];
    if (result) {
        NSLog(@"添加成功");
        
    }else{
        NSLog(@"添加失败");
    }
    
}

- (void)insertDailyModel:(ShouChangModel *)model{
    //spl语句
    
    NSString *splStr = [NSString stringWithFormat:@"insert into ShouChangModel(backImageStr , hearderImageStr , titleLabelStr , nameLabelStr , spot_id) values('%@', '%@', '%@', '%@', '%@')", model.backImageStr, model.hearderImageStr, model.titleLabelStr, model.nameLabelStr, model.spot_id];
    //执行
    BOOL result = [_dbPoint executeUpdate:splStr];
    if (result) {
        NSLog(@"添加成功");
        
    }else{
        NSLog(@"添加失败");
    }

}



- (void)deleteModel:(DBsearchModel *)model{
    NSString *splStr = [NSString stringWithFormat:@"delete from DBsearchModel where searchText = '%@'", model.searchText];
    BOOL result = [_dbPoint executeUpdate:splStr];
    if (result) {
        NSLog(@"删除成功");
    }else{
        NSLog(@"删除失败");
    }

}

//删除登陆数据库数据

- (void)deleteDengLuModel:(DengLuTabelModel *)model{
    
    NSString *splStr = [NSString stringWithFormat:@"delete from DengLuTabelModel where userName = '%@'", model.userName];
    BOOL result = [_dbPoint executeUpdate:splStr];
    if (result) {
        NSLog(@"删除成功");
    }else{
        NSLog(@"删除失败");
    }
    
    
    
}


//删除收藏数据库内容
- (void)deleteDailyModel:(ShouChangModel *)model{
    
    NSString *splStr = [NSString stringWithFormat:@"delete from ShouChangModel where titleLabelStr = '%@'", model.titleLabelStr];
    BOOL result = [_dbPoint executeUpdate:splStr];
    if (result) {
        NSLog(@"删除成功");
    }else{
        NSLog(@"删除失败");
    }
    
    
}



- (void)updateModel:(DBsearchModel *)model{
     NSString *sqlStr = [NSString stringWithFormat:@"update  DBsearchModel set searchText = '%@' where searchText = '%@'", model.searchText, model.searchText];
    BOOL result = [_dbPoint executeUpdate:sqlStr];
    if (result) {
        NSLog(@"更新成功");
    }else{
        NSLog(@"更新失败");
    }
}

- (NSMutableArray *)selectTable{
    NSString *sqlStr = [NSString stringWithFormat:@"select * from DBsearchModel"];
    //执行语句
    FMResultSet *set = [_dbPoint executeQuery:sqlStr];
    
    NSMutableArray *arr = [NSMutableArray array];
    
    while ([set next]) {
        //取出每个key值
        NSString *searchText = [set stringForColumn:@"searchText"];
       
        
        ///  创建model  给model赋值
        DBsearchModel *model = [DBsearchModel dbSearchText ];
        
        model.searchText = searchText;
        
        //将model添加到数组
        [arr addObject:model];
    }
    return arr;
}

- (NSMutableArray *)selectDengLuTable{
    NSString *sqlStr = [NSString stringWithFormat:@"select * from DengLuTabelModel"];
    //执行语句
    FMResultSet *set = [_dbPoint executeQuery:sqlStr];
    
    NSMutableArray *arr = [NSMutableArray array];
    
    while ([set next]) {
        //取出每个key值
        NSString *userName = [set stringForColumn:@"userName"];
        NSString *iconURL = [set stringForColumn:@"iconURL"];
        ///  创建model  给model赋值
        DengLuTabelModel *model = [[DengLuTabelModel alloc] init];
        
        model.userName = userName;
        model.iconURL = iconURL;
        //将model添加到数组
        [arr addObject:model];
    }
    return arr;

}

- (NSMutableArray *)selectDailyTable{
    NSString *sqlStr = [NSString stringWithFormat:@"select * from ShouChangModel"];
    //执行语句
    FMResultSet *set = [_dbPoint executeQuery:sqlStr];
    
    NSMutableArray *arr = [NSMutableArray array];
    
    while ([set next]) {
        //取出每个key值
        NSString *backImageStr = [set stringForColumn:@"backImageStr"];
       NSString *hearderImageStr = [set stringForColumn:@"hearderImageStr"];
        NSString *titleLabelStr = [set stringForColumn:@"titleLabelStr"];
        NSString *nameLabelStr = [set stringForColumn:@"nameLabelStr"];
        NSString *spot_id = [set stringForColumn:@"spot_id"];
    
        ///  创建model  给model赋值
        ShouChangModel *model = [[ShouChangModel alloc] init];
        
        model.backImageStr = backImageStr;
        model.hearderImageStr = hearderImageStr;
        model.titleLabelStr = titleLabelStr;
        model.nameLabelStr = nameLabelStr;
        model.spot_id = spot_id;
        
        
        
        
        //将model添加到数组
        [arr addObject:model];
    }
    return arr;

}

- (void)dealloc{
    [_model release];
    [super dealloc];
}


@end
